﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ServiceApplication.Service.Basic;
using OAclient.Common;
using System.IO;
using CommonLibrary.DataModel.Basic;
using ServiceApplication.Service.WorkFlow;
using System.Data;

namespace OAclient.Controllers
{
    public class CommonController : Controller
    {

        BasicService bs = new BasicService();
        WorkFlowService wfs = new WorkFlowService();

        public ActionResult UserTree()
        {
            //string s = ZTreeHelper.Serialize(bs.GetWebUserNodeList());
            //return s;

            return View();
        }

        public ActionResult StaffTree()
        {
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="nocheck"></param>
        /// <returns>json格式</returns>
        public ActionResult getUserTreeByDept(string nocheck)
        {
            bs.QueryAllDepts();
            bs.QueryAllDutys();
            bs.QueryAllUsers();
            return Json(null);
        }

        public ActionResult getUserTreeByDuty(string nocheck)
        {
            return Json(null);
        }


        public ActionResult GetImage(String fileName)
        {
            string mime = FileHelper.GetFileMIME(Path.GetExtension(fileName));
            String filePath = Path.Combine(storeUrl, fileName);
            return File(new FileStream(filePath, FileMode.Open, FileAccess.Read), mime, fileName);
        }


        String storeUrl = Path.Combine(ComVariable.StoreUrl, "webstore");
        //为手机端网盘功能提供下载接口
        public HttpResponseBase WebStoreDownloadFile(int fileid)
        {
            FileEntity file = bs.FileQuery(fileid);
            //if (file == null) return Content("文件不存在");

            String filePath = Path.Combine(storeUrl, file.file_replacename + file.file_ext);
            //if (!System.IO.File.Exists(filePath)) return Content("文件不存在");

            //return File(new FileStream(filePath, FileMode.Open, FileAccess.Read), file.file_mime, file.file_replacename + file.file_ext);
            //FileStream fsa = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            //int b1;
            //System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
            //while ((b1 = fsa.ReadByte()) != -1)
            //{
            //    tempStream.WriteByte(((byte)b1));
            //}

            //return tempStream.ToArray();


            //以字符流的形式下载文件
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
            Response.AddHeader("Content-Transfer-Encoding", "binary");
            Response.AddHeader("Content-Length", fs.Length.ToString());
            Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode(file.file_name, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
            fs.Close();
            fs.Dispose();
            return Response;
        }

    }
}
